package com.whfc.fse.manager.impl;

import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.AppWarnModuleType;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.entity.dto.msg.AppMsgToUserDTO;
import com.whfc.entity.dto.msg.PushMsgDTO;
import com.whfc.fse.dao.FseWarnMapper;
import com.whfc.fse.dao.FseWarnRuleChannelMapper;
import com.whfc.fse.dao.FseWarnRuleMapper;
import com.whfc.fse.dao.FseWarnRuleUserMapper;
import com.whfc.fse.dto.FseWarnDTO;
import com.whfc.fse.entity.FseWarn;
import com.whfc.fse.enums.FseType;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.manager.FseWarnMgr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021/11/2 10:59
 */
@Component
public class FseWarnMgrImpl implements FseWarnMgr {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String CONTENT = "%s在%s,%s,请您及时处理";

    @Autowired
    private FseWarnMapper fseWarnMapper;

    @Autowired
    private FseWarnRuleMapper fseWarnRuleMapper;

    @Autowired
    private FseWarnRuleChannelMapper fseWarnRuleChannelMapper;

    @Autowired
    private FseWarnRuleUserMapper fseWarnRuleUserMapper;

    @Resource(name = "baseRabbitTemplate")
    private AmqpTemplate amqpTemplate;

    @Override
    public void handleFseWarn(FseWarnDTO fseWarnData) {
        logger.info("接收报警数据：{}",fseWarnData);
        Integer deptId = fseWarnData.getDeptId();
        FseType fseType = fseWarnData.getFseType();
        FseWarnRuleType fseWarnRuleType = fseWarnData.getFseWarnRuleType();
        String triggerValue = fseWarnData.getTriggerValue();
        Date triggerTime = fseWarnData.getTriggerTime();
        Integer triggerObjectId = fseWarnData.getTriggerObjectId();
        String code = fseWarnData.getCode();
        //查询报警规则ID
        Integer ruleId = fseWarnRuleMapper.selectByDeptIdAndRuleType(deptId, fseWarnRuleType.value());
        if (ruleId == null && !StringUtils.hasText(fseWarnData.getTriggerValue())) {
            logger.info("==========未获取到报警信息============");
            return;
        }

        // 插入报警
        FseWarn fseWarn = new FseWarn();
        fseWarn.setDeptId(deptId);
        fseWarn.setWarnRuleId(ruleId==null?-1:ruleId);
        fseWarn.setRuleType(fseWarnRuleType.value());
        fseWarn.setTriggerObjectId(String.valueOf(triggerObjectId));
        fseWarn.setTriggerTime(triggerTime);
        fseWarn.setTriggerValue(triggerValue);
        fseWarnMapper.insertSelective(fseWarn);
        Integer fseWarnId = fseWarn.getId();

        // 查找报警通知方式
        List<Integer> channelList = fseWarnRuleChannelMapper.selectByRuleId(ruleId);
        if (channelList.isEmpty()) {
            return;
        }
        // 查找报警通知人
        List<AppMsgToUserDTO> userList = fseWarnRuleUserMapper.selectByRuleId(ruleId);
        if (userList.isEmpty()) {
            return;
        }

        //推送消息
        String ruleStr = fseWarnRuleType.detail();
        String title = fseWarnRuleType.tittle();
        String timeStr = DateUtil.formatDate(triggerTime, "yyyy-MM-dd HH:mm:ss");
        String content = String.format(CONTENT, fseType.getDesc() + code, timeStr, ruleStr==null?fseWarn.getTriggerValue():ruleStr);

        //推送消息
        PushMsgDTO pushMsgDTO = new PushMsgDTO();
        pushMsgDTO.setDeptId(deptId);
        pushMsgDTO.setModuleType(AppWarnModuleType.FSE.value());
        pushMsgDTO.setTime(triggerTime);
        pushMsgDTO.setTitle(title);
        pushMsgDTO.setContent(content);
        pushMsgDTO.setMsgObjectId(String.valueOf(fseWarnId));
        pushMsgDTO.setMsgChannelList(channelList);
        pushMsgDTO.setToUserList(userList);

        amqpTemplate.convertAndSend(QueueConst.PUSH_MSG_EXCHANGE, "", JSONUtil.toString(pushMsgDTO));
    }
}
